package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{

    @Autowired
    public OrderSettingMapper orderSettingMapper;

    @Override
    public Result upload(List<String[]> file) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        try {
            for (String[] dataAndNum : file) {
                Date date = simpleDateFormat.parse(dataAndNum[0]);
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(Integer.parseInt(dataAndNum[1]));
                Integer upload = this.orderSettingMapper.upload(orderSetting);
                if (upload != 1){
                    return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
                }
            }
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
    }
    @Override
    @Transactional
    public void saveOrEdit(OrderSetting orderSetting) {
        OrderSetting orderSetting1 = orderSettingMapper.findByOrderDate(orderSetting.getOrderDate());
        if (orderSetting1 == null) {
            orderSettingMapper.add(orderSetting);
        }
        if (orderSetting1.getReservations() > orderSetting.getNumber()) {
            throw new BusinessRuntimeException("已经预约人数不能大于可预约人数");
        } else {
            orderSettingMapper.edit(orderSetting);
        }
    }
    //根据日期查询预约设置数据
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }
}
